// 配置axios
import { useChannelStore } from '@/store/channel'
import type { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'
import axios from 'axios'

// 创建axios实例
const instance = axios.create({
  baseURL: '/pc/visitor', //设置API的基础URL
  timeout: 3000,
})

// 请求拦截器
instance.interceptors.request.use(
  (config: AxiosRequestConfig): any => {
    const channel = useChannelStore()
    //  获取请求头
    const headers = config.headers || {}
    headers['X-Tenant'] = channel.SystemInfo.tenantId
    config.headers = headers
    return config
  },
  (error: AxiosError) => {
    // 处理请求错误
    return Promise.reject(error)
  },
)
// 响应拦截器
instance.interceptors.response.use(
  (response: AxiosResponse) => {
    // 对响应数据进行处理
    switch (response.status) {
      case 200:
        return response.data
      case 401:
        break
      case 403:
        break
      case 404:
        break
      case 500:
        break
    }
  },
  (error: AxiosError) => {
    // 处理响应错误
    return Promise.reject(error)
  },
)

// 导出axios请求方法
// GET
export function GET<T>(url: string, params?: any) {
  return instance.get<T>(url, { params })
}
// POST
export function POST<T>(url: string, data?: any, config?: AxiosRequestConfig) {
  return instance.post<T>(url, data, config)
}
// PUT
export function PUT<T>(url: string, data?: any) {
  return instance.put<T>(url, data)
}
//Delete
export function Delete<T>(url: string) {
  return instance.delete<T>(url)
}
